using UnityEngine;

public class MeshAnimManager : MonoBehaviour
{
    public GameObject m_leftRoot;
    public GameObject m_rightRoot;

    private MeshAnimBase[] _leftMeshAnims;
    private MeshAnimBase[] _rightMeshAnims;

    private void Awake()
    {
        InitAnimation();
    }

    private void InitAnimation()
    {
        if (m_leftRoot != null) _leftMeshAnims = m_leftRoot.GetComponentsInChildren<MeshAnimBase>();
        if (m_rightRoot != null) _rightMeshAnims = m_rightRoot.GetComponentsInChildren<MeshAnimBase>();
    }

    private void Start()
    {
        EventCenter.CameraEvent.CameraReachAngleEvent += CameraReachAngle;
    }

    private void OnDestroy()
    {
        EventCenter.CameraEvent.CameraReachAngleEvent -= CameraReachAngle;
    }

    private void CameraReachAngle(int angleType, bool reach)
    {
        switch (angleType)
        {
            case 0:
                StartLeftAnimation(reach);
                break;
            case 1:
                StartRightAnimation(reach);
                break;
        }
    }

    private void StartLeftAnimation(bool forward)
    {
        if (_leftMeshAnims == null) return;
        var count = _leftMeshAnims.Length;
        for (var i = 0; i < count; i++)
        {
            _leftMeshAnims[i].StartAnimation(forward);
        }
    }

    private void StartRightAnimation(bool forward)
    {
        if (_rightMeshAnims == null) return;
        var count = _rightMeshAnims.Length;
        for (var i = 0; i < count; i++)
        {
            _rightMeshAnims[i].StartAnimation(forward);
        }
    }
}
